/*
 * Copyright 2018 Youchaoshi authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package common.plugins.tbs;

import android.app.Activity;
import android.view.View;
import android.widget.ProgressBar;

import com.tencent.smtt.export.external.interfaces.JsResult;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;

/**
 * WebChromeClient重写
 * Created by JustTong on 2018/12/11.
 */
public class WebCoreChromeClient extends WebChromeClient {
    Activity mActivity;
    ProgressBar mPbBar;
    String mTitle;

    public WebCoreChromeClient(Activity activity, ProgressBar pbBar, String title) {
        super();

        this.mActivity = activity;
        this.mPbBar = pbBar;
        this.mTitle = title;


    }

    @Override
    public void onReceivedTitle(WebView view, String title) {
        if (mTitle.equals("")) {
            mActivity.setTitle(title);
        } else {
            mActivity.setTitle(mTitle);
        }

    }

    @Override
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        WebCoreJsInterface.Dialogshow(message);
        result.confirm();

        return true;
    }

    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        // TODO Auto-generated method stub
        mPbBar.setProgress(newProgress);

        if (mPbBar != null && newProgress != 100) {
            mPbBar.setVisibility(View.VISIBLE);
        } else {
            mPbBar.setVisibility(View.GONE);
        }


    }
}
